import time, os

v_data = []
a_data = []
s_data = []
vidx = 0
sidx = 0

SCREEN_WITHOUT_AUDIO = 1
SCREEN_WITH_AUDIO = 2
MOVI_BLOCK_INDEX = 2048

class FileReader:

    def __init__(self, video, screen):
        self.video = video
        self.screen = screen
        self.type = self.checkType()

    def checkType(self):
        screenfile = open(self.screen, "rb")
        screenheader = screenfile.read(2048)    # read the header
        screenstreams = screenheader[56]        # dwstreams

        if screenstreams == '\x01':
            return SCREEN_WITHOUT_AUDIO
        else:
            return SCREEN_WITH_AUDIO

    def readVideoData(self, index):
        global vidx
        if index == 0:
            idx = MOVI_BLOCK_INDEX
        else:
            idx = index

        global v_data, a_data
        fileSize = os.path.getsize(self.video)
        vf = open(self.video,"rb")
        text = vf.read(fileSize + 100)
        vc = 0
        v_data = []
        if self.type == SCREEN_WITHOUT_AUDIO:
            a_data = []

        while (vc < 3000):        # 5 minutes' data
            if text[idx:idx+4] == "01wb" and self.type == SCREEN_WITHOUT_AUDIO:    # audio frame
                frameSize = 16 ** 2 * ord(text[idx+5]) + ord(text[idx+4])    # compute frame size
                if frameSize > 0:
                    a_data.append(text[idx+8:idx+frameSize+8])    # 4 bits for type and 4 bits for length
                else:
                    a_data.append("")
                idx += frameSize + 8
            elif text[idx:idx+4] == "01wb" and self.type == SCREEN_WITH_AUDIO:    # ignore audio
                idx += frameSize + 8
            elif text[idx:idx+4] == "00dc":        # video data
                frameSize = 16 ** 2 * ord(text[idx+5]) + ord(text[idx+4])
                if frameSize > 0:
                    v_data.append(text[idx+12:idx+frameSize+8])
                    vc += 1
                else:        # empty frame
                    v_data.append("")
                    vc += 1
                idx += frameSize + 8
            elif text[idx:idx+4] == "00db":    # key frame
                frameSize = 16 ** 2 * ord(text[idx+5]) + ord(text[idx+4])
                v_data.append(text[idx+12:idx+frameSize+8])
                idx += frameSize + 8
                vc += 1
            else:
                if text[idx] == '\x00':        # padding byte, just ignore
                    idx += 1
                else:        # end of file
                    vf.close
                    vidx = 0
                    return

        vf.close()
        vidx = idx

    def readScreenData(self, index):
        global sidx

        if index == 0:
            idx = MOVI_BLOCK_INDEX
        else:
            idx = index

        global s_data, a_data
        fileSize = os.path.getsize(self.screen)
        sf = open(self.screen,"rb")
        text = sf.read(fileSize + 100)
        sc = 0

        s_data = []
        if self.type == SCREEN_WITH_AUDIO:
            a_data = []

        while (sc < 3000):
            if text[idx:idx+4] == "01wb":        # audio frame
                frameSize = 16 ** 2 * ord(text[idx+5]) + ord(text[idx+4])
                if frameSize > 0:
                    a_data.append(text[idx+8:idx+frameSize+8])
                else:
                    a_data.append("")
                idx += frameSize + 8
            elif text[idx:idx+4] == "00dc" or text[idx:idx+4] == "00db":      # screen frame
                frameSize = 16 ** 2 * ord(text[idx+5]) + ord(text[idx+4])
                s_data.append(text[idx+8:idx+frameSize+8])
                idx += frameSize + 8
                sc += 1
            else:
                if text[idx] == '\x00':
                    idx += 1
                else:
                    sf.close()
                    sidx = 0
                    return

        sf.close()
        sidx = idx